var Buffer = function() {
    this.__class__ = arguments.callee;
    this.initialize.apply(this, arguments);
};

Buffer.prototype = {
    initialize: function() {
        this.vec = [];
        this.length = 0;
        this.anchor = 0;
    },

    seek: function(anchor) {
        this.anchor = anchor;
    },

    U8: function(v) {
        if (this.anchor != this.vec.length) {
            if (this.vec[this.anchor].length != 1)
                throw new this.Exception();
        } else {
            this.length++;
        }
        this.vec[this.anchor++] = String.fromCharCode(v);
    },

    U16LE: function(v) {
        if (this.anchor != this.vec.length) {
            if (this.vec[this.anchor].length != 2)
                throw new this.Exception();
        } else {
            this.length += 2;
        }
        this.vec[this.anchor++] = String.fromCharCode(v & 255)
                      + String.fromCharCode((v >> 8) & 255);
    },

    U16BE: function(v) {
        if (this.anchor != this.vec.length) {
            if (this.vec[this.anchor].length != 2)
                throw new this.Exception();
        } else {
            this.length += 2;
        }
        this.vec[this.anchor++] = String.fromCharCode((v >> 8) & 255)
                      + String.fromCharCode(v & 255);
    },

    U32LE: function(v) {
        if (this.anchor != this.vec.length) {
            if (this.vec[this.anchor].length != 4)
                throw new this.Exception();
        } else {
            this.length += 4;
        }
        this.vec[this.anchor++] = String.fromCharCode(v & 255)
                      + String.fromCharCode((v >> 8) & 255)
                      + String.fromCharCode((v >> 16) & 255)
                      + String.fromCharCode(v >> 24);
    },

    U32BE: function(v) {
        if (this.anchor != this.vec.length) {
            if (this.vec[this.anchor].length != 4)
                throw new this.Exception();
        } else {
            this.length += 4;
        }
        this.vec[this.anchor++] = String.fromCharCode(v >> 24)
                      + String.fromCharCode((v >> 16) & 255)
                      + String.fromCharCode((v >> 8) & 255)
                      + String.fromCharCode(v & 255);
    },

    S: function(v) {
        if (this.anchor != this.vec.length) {
            if (this.vec[this.anchor].length != v.length)
                throw new this.Exception();
        } else {
            this.length += v.length;
        }
        this.vec[this.anchor++] = v;
    },

    pad: function(l, c) {
        if (this.anchor != this.vec.length) {
            if (this.vec[this.anchor].length != l)
                throw new this.Exception();
        } else {
            this.length += l;
        }
        var retval = new Array(32);
        var i = l, p = c, j = -1;
        for (;;) {
            if (i & 1)
                retval[++j] = p;
            if (!i)
                break;
            p += p;
            i >>= 1;
        }
        this.vec[this.anchor++] = retval.join('');
    },

    toString: function() {
        return this.vec.join('');
    }
};


